-- PixyCage
-- Created by chengb Feb/3/2016
-- 妖精的铁笼格子

-- 初始化
function initPixyCage(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local dustEffectParent = findChildByName(self.node, "effect_node");
    dustEffectParent:setVisible(true);

    local function okFunc()
        -- self.item:setVisible(false);

        local tip = buildingInfo["bonus_tip"];
        if type(tip) == "string" and tip ~= "" then
            alert(tip);
        end

        -- 放出妖精
        local function rescue()
            local pos = self.gridData:getPos();

            local function delay()
                DungeonActionM.go("open_pixy_cage", pos);
            end

            performWithDelay(self, delay, 0.8);

            local function callback()
                -- 隐藏牢笼图片
                self.item:setVisible(false);

                -- 铁笼打开动画
                playPrisonOpenEffect(self, dustEffectParent);
            end
            performWithDelay(self, callback, 0.1);
        end

        performWithDelay(self, rescue, 0.2);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            if DungeonInstanceM.isInInstance() and
               not DungeonM.isAllMonsterDead() then
                -- 如果在副本中，需要击杀所有怪物方可拾取
                self:openBuildingView(nil, nil, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end

    -- 建筑图标（牢笼图片）
    local iconPath = getLevelItemIconPath("prison");

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 透明处理
    -- self.icon:setOpacity(0);
    -- 播放精灵在笼子内动画
    playPixyLockEffect(self, dustEffectParent);

    -- 标记已经有动画了
    self.hasPixyEffect = true;
end

